<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\10\10 0010
 * Time: 13:24
 */

namespace app\admin\controller;


use app\Lib\Api\Menu\addMenu;
use app\Lib\Api\Menu\editMenu;
use app\Lib\Api\Menu\getMenuAllMenu;
use app\Lib\Api\Menu\getMenuInfo;
use app\Lib\Api\Menu\getMenuList;
use app\Lib\Api\Menu\getUserMenu;
use app\Lib\TopClient;
use app\validate\MenuValidate;
use think\Exception;
use think\Url;

class Menu extends Base
{
    /**
     * @return array
     * @throws \Exception
     */
    public function getMenu(){
        $model =  (new getUserMenu());
        $model->setArgs('userId',$this->login_uid);
        $data = (new TopClient($model))->getSuccessData();
        $list = [];
        foreach ($data as $v){
            if($v['pid'] == '0'){
                $list[$v['mark']] = $this->getHandleArray($data,$v['id']);
            }
        }
        return $list;
    }

    public function addMenu(){
        try{
            if($this->request->isPost()){
                //获取参数
                $data =  input('post.');
                //参数验证
                $model =  new MenuValidate();
                if(!$model->check($data)){
                    throw new Exception($model->getError());
                }
                if($data['id']>0){
                    $model =  new editMenu();
                    $model->setArgs('menuName',$data['name']);
                    $model->setArgs('project',$data['project']);
                    $model->setArgs('controller',$data['c']);
                    $model->setArgs('action',$data['a']);
                    $model->setArgs('parentId',$data['parent_id']);
                    $model->setArgs('sortNo',$data['sort']);
                    $model->setArgs('showMenu',$data['show']);
                    $model->setArgs('menuClass',$data['menu_class']);
                    $model->setArgs('status',$data['status']);
                    $model->setArgs('mark',$data['mark']);
                    $model->setArgs('opUid',$this->login_uid);
                    $model->setArgs('id',$data['id']);
                    (new TopClient($model))->getSuccessData();
                }else{
                    //请求接口
                    $model =  new addMenu();
                    $model->setArgs('menuName',$data['name']);
                    $model->setArgs('project',$data['project']);
                    $model->setArgs('controller',$data['c']);
                    $model->setArgs('action',$data['a']);
                    $model->setArgs('parentId',$data['parent_id']);
                    $model->setArgs('sortNo',$data['sort']);
                    $model->setArgs('showMenu',$data['show']);
                    $model->setArgs('menuClass',$data['menu_class']);
                    $model->setArgs('status',$data['status']);
                    $model->setArgs('mark',$data['mark']);
                    $model->setArgs('opUid',$this->login_uid);
                    (new TopClient($model))->getSuccessData();
                }
                return ['code'=>1,'msg'=>'登录成功','url'=>Url::build('admin/index/index')];
            }else{
                $menu_id = input('menu_id','0');
                $menu_info = $this->getMenuInfo($menu_id);
                //父级菜单
                $parentMenu = $this->parentMenu();
                $this->assign('parentMenu',$parentMenu);
                //父级菜单标识
                $this->assign('menuInfo',$menu_info);
                return $this->fetch();
            }
        }catch (\Exception $e){
            return ['code'=>-1,'msg'=>$e->getMessage()];
        }
    }

    /**
     * @param int $menu_id
     * @return array|string
     * @throws \Exception
     */
    private function getMenuInfo($menu_id = 0){
        if($menu_id<=0 ){
            $result = [
                'id'=>0,
                'name'=>'',
                'project'=>'',
                'c'=>'',
                'a'=>'',
                'menu_class'=>'',
                'parent_id'=>0,
                'show_menu'=>'',
                'mark'=>'',
                'sort_no'=>0,
                'status'=>''
            ];
            return $result;
        }
        $model = (new getMenuInfo());
        $model->setArgs('menu_id',$menu_id);
        $result = (new TopClient($model))->getSuccessData();
        return $result;
    }

    /**
     * @return array
     * @throws \Exception
     */
    private function parentMenuMake(){
        $data =  $this->getMenuList();
        $list = ['0'=>'顶级'];
        if(empty($data)){
            return $list;
        }

        foreach($data as $v){
            $list[$v['mark']] = $v['name'];
        }

        return $list;
    }

    /**
     * @return array
     * @throws \Exception
     */
    private function parentMenu(){
        $data =  $this->getMenuList();
        $list = ['0'=>'顶级菜单'];
        if(empty($data)){
            return $list;
        }
        foreach($data as $v){
            $list[$v['id']] = $v['name'];
        }

        return $list;
    }



    private function getHandleArray(&$result,$pid = 0){
        $data = [];
        foreach ($result as $v){
            $parentId =$v['pid'];
            $parentId =empty($parentId) ||$parentId == '无' ?0:$parentId;
            if($parentId == $pid){
                $children = $this->getHandleArray($result,$v['id']);
                $v['href'] = Url::build( $v['href']);
                $array = $v;
                if(!empty($children)){
                    $array['children'] = $children;
                }
                $data[] = $array;
            }
        }
        return $data;
    }

    public function index(){
        $keyword = input('keyword');
        $this->assign('keyword',$keyword);
        return $this->fetch();
    }

    /**
     * @return array|string
     * @throws \Exception
     */
    private function getMenuList(){
        $model =  new getMenuAllMenu();
        $topClient = new TopClient($model);
        $data = $topClient->getSuccessData();
        return $data;
    }

    public function getList(){
        try{
            $page = input('page');
            $limit = input('limit');
            $keyword = input('keyword');
            $model = new getMenuList();
            $model->setArgs('page',$page);
            $model->setArgs('limit',$limit);
            $model->setArgs('keyword',$keyword);
            $model->setArgs('keywordType','name');
            $result = (new TopClient($model))->getSuccessData();
            $this->assign('result',$result);
            return $this->fetch('/menu/ajaxList');
        }catch (\Exception $e){
            dump($e->getMessage());
        }

    }

    public function getPage(){
        $page = input('page',1);
        $limit = input('limit',10);
        $keyword = input('keyword');
        $model = new getMenuList();
        $model->setArgs('page',$page);
        $model->setArgs('limit',$limit);
        $model->setArgs('keyword',$keyword);
        $model->setArgs('keywordType','name');
        $model->setArgs('isPage',true);
        $result = (new TopClient($model))->getSuccessData();
        $this->assign('result',$result);
        return $result;
    }
}